<!DOCTYPE html>
<html>
	{include file="public/_header" /}
	<body>
		<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="">首页</a>
				<a>
					<cite>在线留言</cite></a>
			</span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()"
			 title="刷新">
				<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
		</div>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-body ">
							<table class="layui-hide" id="test" lay-filter="test"></table>
						</div>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
					<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="getCheckLength">
						<i class="layui-icon">&#xe642;</i>{:lang('delete')}
					</button>
				</div>
		</script>

		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="view">{:lang('view')}</a>
			<a class="layui-btn layui-btn-xs" lay-event="reply">{:lang('reply')}</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('delete')}</a>
		</script>

		<script>
			function check_validate1(value) {
				//定义正则表达式部分
				var reg = /^\d+$/;
				if (value.match(reg)) {
					return true;
				}
				return false;
			}
			var url = "__STATIC__/admin/"
			layui.use(['util', 'laydate', 'table'], function() {
				var table = layui.table;
				var form = layui.form;
				var util = layui.util;
				var tableIns = table.render({
					elem: '#test',
					url: "{:url('admin/Guestbook/index_lists')}",
					toolbar: '#toolbarDemo',
					cols: [
						[{
							type: 'checkbox',
							fixed: 'left',
							width: 80
						}, {
							field: 'id',
							title: 'ID',
							width: 80,
							fixed: 'left',
							unresize: true,
							sort: true
						}, {
							field: 'listorder',
							title: "{:lang('sort')}",
							width: 100,
							edit: true,
							sort: true
						}, {
							field: 'title',
							title: "{:lang('title')}"
						}, {
							field: 'content',
							title: "{:lang('content')}"
						}, {
							field: 'inputtime',
							title: "{:lang('inputtime')}",
							sort: true,
							templet: function(d) {
								var logoSrc = util.toDateString(d.inputtime * 1000, "yyyy-MM-dd HH:mm:ss");
								return logoSrc;
							}
						}, {
							field: 'updatetime',
							title: "{:lang('updatetime')}",
							templet: function(d) {
								var logoSrc = util.toDateString(d.updatetime * 1000, "yyyy-MM-dd HH:mm:ss");
								return logoSrc;
							},
							sort: true
						}, {
							field: 'status',
							title: "{:lang('verify')}",
							templet: function(d) {
								if (d.status == 1) {
									var check = "<input type='checkbox' value='" + d.id +"' lay-filter='status' lay-skin='switch' lay-text='{:lang("show")}|{:lang("hide")}' checked>";
								} else if (d.status == 0) {
									var check = "<input type='checkbox' value='" + d.id +"' lay-filter='status' lay-skin='switch' lay-text='{:lang("show")}|{:lang("hide")}'>";
								}

								return check;
							}
						}, {
							fixed: 'right',
							title: "{:lang('action')}",
							toolbar: '#barDemo',
							width: 180
						}]
					],
					id: 'testReload',
					page: true
				});

				//头工具栏事件
				table.on('toolbar(test)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'getCheckLength':
							var data = checkStatus.data;

							if (data.length < 1) {
								layer.msg('您最少选择一个选项进行操作');
								return false;
							}
							var ids = [];
							data.forEach(function(item, index) {
								ids.push(item.id)
							})
							parent.layer.confirm('确认要删除这些内容？', function() {
								$.post("{:url('admin/Guestbook/all_dele')}", {
									ids: ids
								}, function(res) {
									if (res.status == 1) {
										parent.layer.msg('删除成功', {
											icon: 1,
											time: 300
										}, function() {
											tableIns.reload()
										});
									} else {
										parent.layer.msg(res.message)
									}
								})
							});

							break;
					};
				});

				//监听行工具事件
				table.on('tool(test)', function(obj) {
					var data = obj.data;
					//console.log(obj)
					if (obj.event === 'del') {
						parent.layer.confirm('真的删除行么', function(index) {
							$.post("{:url('admin/Guestbook/dele')}", data, function(res) {
								if (res.status == 1) {
									parent.layer.msg('删除成功', {
										icon: 1,
										time: 300
									}, function() {
										parent.layer.close(index);
										tableIns.reload()
									});
								} else {
									parent.layer.msg(res.message)
								}
							})

						});
					} else if (obj.event === 'reply') {
						parent.layer.open({
							title: "{:lang('reply')}",
							type: 2,
							area: ['800px', '680px'],
							content: "{:url('admin/Link/linkedit')}?id=" + data.id,
							success: function() {
								top.layerFrameConfig.iframeB = {
									info: tableIns
								}
							}
						});
					}
				});
				//监听单元格编辑
				table.on('edit(test)', function(obj) {
					var value = obj.value, //得到修改后的值
						data = obj.data, //得到所在行所有键值
						field = obj.field; //得到字段
					if (!check_validate1(value)) {
						layer.msg('你必须输入数字');
						tableIns.reload()
					} else {
						sort(obj, tableIns)
					}


				});
				form.on('switch(status)', function(obj) {

					layer.tips("审核通过可以在前台显示", obj.othis, {
						tips: [1, '#3595CC'],
						time: 4000
					});
					var data = {
						id: this.value,
						status: obj.elem.checked ? 1 : 0
					}
					$.post("{:url('admin/Guestbook/is_check')}", data, function(res) {
						if (res.status == 1) {
							layer.msg('设置成功');
						} else {
							layer.msg(res.message);
							tableIns.reload()
						}
					})
				});
			});

			function sort(obj, tableIns) {
				$.post("{:url('admin/Guestbook/sort')}", obj.data, function(res) {
					if (res.status == 1) {
						tableIns.reload()
					}
				})
			}
		</script>
	</body>
</html>
